//营业税发票
var BTInv = {};

/*当获取票据数据时，特殊操作*/
BTInv.initPanel = function(json){	 	
 	//营业税发票处理
     var invConfig = Share.invoiceInitConfig(json.DOC_TYPE_CODE);//获取对应票据设置 
     this.panelHideShow(invConfig);
};

/*当票据类型转换时，对应操作*/
BTInv.changeInvPanel = function(type){ 	
	var invConfig = Share.invoiceInitConfig(type);//获取对应票据设置 
	/*1.票据对应计价方式转换*/    
    Ext.getCmp('deal-enter-mode').setValue(invConfig.PRICE_WAY.toString());//设置价格方式默认值
	if(invConfig.PRICE_WAY == '1'){//"含税"
		CountMode = 1;	   
		var store = Ext.getCmp('enter-invoice-grid').getStore();
		for(var i=0;i<store.getCount();i++){
			var A = parseFloat(store.getAt(i).get('amount'));
    		var R = parseFloat(store.getAt(i).get('rate'));
    		var DR = parseFloat(store.getAt(i).get('discount_rate'))/100;
    		store.getAt(i).set('tax',Share.RetainDecimal(A/(1+R)*R));
    		store.getAt(i).set('discount_tax',Share.RetainDecimal(A/(1+R)*R*DR));
		}
	}else{//“不含税”
		CountMode = 0;
		var store = Ext.getCmp('enter-invoice-grid').getStore();
		for(var i=0;i<store.getCount();i++){
			var A = parseFloat(store.getAt(i).get('amount'));
    		var R = parseFloat(store.getAt(i).get('rate'));
    		var DR = parseFloat(store.getAt(i).get('discount_rate'))/100;
    		store.getAt(i).set('tax',Share.RetainDecimal(A*R));
    		store.getAt(i).set('discount_tax',Share.RetainDecimal(A*R*DR));
		}
	}
	/*2.界面显示隐藏*/
    this.panelHideShow(invConfig);
};

/*保存票据*/
BTInv.save = function(){
	var store = Ext.getCmp('enter-invoice-grid').getStore();
	var count = Ext.getCmp("invoice_count").getValue();
	var head_id = Ext.getCmp('head_id').getValue();
	var num = Ext.getCmp('deal-enter-num').getValue();
	var date = Ext.getCmp('deal-enter-date').getValue();
	var type = Ext.getCmp('deal-enter-type').getValue();
	var mode = Ext.getCmp('deal-enter-mode').getValue();
	var custom = Ext.getCmp('deal-buyer-name').getValue();
	var custom_tax = Ext.getCmp('deal-buyer-taxpayer').getValue();
	var address = Ext.getCmp('deal-buyer-address').getValue();
	var account = Ext.getCmp('deal-buyer-account').getValue();
	var custCode = Ext.getCmp('deal-buyer-code').getValue();
	var remark = Ext.getCmp('deal-enter-remark').getValue();
	var seller = Ext.getCmp('deal-seller-name').getValue();
	var seller_tax = Ext.getCmp('deal-seller-taxpayer').getValue();
	var seller_address = Ext.getCmp('deal-seller-address').getValue();
	var seller_account = Ext.getCmp('deal-seller-account').getValue();
	var field1 = Ext.getCmp('deal-enter-field1').getValue();
	var field2 = Ext.getCmp('deal-enter-field2').getValue();
	var field3 = Ext.getCmp('deal-enter-field3').getValue();
	var field4 = Ext.getCmp('deal-enter-field4').getValue();
	var field5 = Ext.getCmp('deal-enter-field5').getValue();
	var field6 = Ext.getCmp('deal-enter-field6').getValue();
	var field7 = Ext.getCmp('deal-enter-field7').getValue();
	var field8 = Ext.getCmp('deal-enter-field8').getValue();
	var field9 = Ext.getCmp('deal-enter-field9').getValue();
	var field10 = Ext.getCmp('deal-enter-field10').getValue();
	var field11 = Ext.getCmp('deal-enter-field11').getValue();
	var field12 = Ext.getCmp('deal-enter-field12').getValue();
	var field13 = Ext.getCmp('deal-enter-field13').getValue();
	var field14 = Ext.getCmp('deal-enter-field14').getValue();
	var field15 = Ext.getCmp('deal-enter-field15').getValue();
	var field16 = Ext.getCmp('deal-enter-field16').getValue();
	var field17 = Ext.getCmp('deal-enter-field17').getValue();
	var field18 = Ext.getCmp('deal-enter-field18').getValue();
	var field19 = Ext.getCmp('deal-enter-field19').getValue();
	var field20 = Ext.getCmp('deal-enter-field20').getValue();
	var payee = Ext.getCmp('deal-enter-payee').getValue();
	var checker = Ext.getCmp('deal-enter-checker').getValue();            	
	var drawer = Ext.getCmp('deal-enter-drawer').getValue();
	var totalLine = store.getCount();
	//以上获取HEAD信息
//	var exp_no = Ext.getCmp('exp-contact-no').getValue();
//	var exp_method = Ext.getCmp('exp-trade-method').getValue();
//	var exp_form = Ext.getCmp('exp-collection-form').getValue();
//	var exp_lcNo = Ext.getCmp('exp-lc-no').getValue();
//	var exp_rage = Ext.getCmp('exp-exchange-rage').getValue();
//	var exp_transport = Ext.getCmp('exp-transport').getValue();
//	var exp_fromAddress = Ext.getCmp('exp-from-address').getValue();
//	var exp_toAddress = Ext.getCmp('exp-to-address').getValue();
//	var exp_currency = Ext.getCmp('exp-currency').getValue();
//	var exp_seller = Ext.getCmp('exp-seller').getValue();
//	var exp_sellerAddress = Ext.getCmp('exp-seller-address').getValue();
//	var exp_sellerTel = Ext.getCmp('exp-seller-tel').getValue();
//	var exp_sellerFax = Ext.getCmp('exp-seller-fax').getValue();
//	
//	//以上获取进出口信息            	
	var detail_ids = [];
	var tax_items = [];
	var goods_names=[];
	var goods_codes=[];
	var specs = [];
	var goods_units=[];
	var quantitys=[];
	var price_cures=[];
	var amount_curs=[];
	var tax_rates=[];
	var tax_amounts=[];
	var discount_rates=[];
	var discount_sums=[];
	var discount_taxs=[];

	for(var i = 0;i<store.getCount();i++){
		detail_ids[detail_ids.length] = store.getAt(i).get('detail_id');
		tax_items[tax_items.length] = store.getAt(i).get('goods_tax');
		goods_names[goods_names.length] = store.getAt(i).get('goods_name');
		goods_codes[goods_codes.length] = store.getAt(i).get('goods_code');
		specs[specs.length] = store.getAt(i).get('specification');
		goods_units[goods_units.length] = store.getAt(i).get('unit');
		quantitys[quantitys.length] = store.getAt(i).get('quantity') == "" ? 0 : store.getAt(i).get('quantity');
		price_cures[price_cures.length] = store.getAt(i).get('price') == "" ? 0 : store.getAt(i).get('price');
		amount_curs[amount_curs.length] = store.getAt(i).get('amount') == "" ? 0 : store.getAt(i).get('amount');
		tax_rates[tax_rates.length] = store.getAt(i).get('rate') == "" ? 0 : store.getAt(i).get('rate');
		tax_amounts[tax_amounts.length] = store.getAt(i).get('tax') == "" ? 0 : store.getAt(i).get('tax');
		discount_rates[discount_rates.length] = parseFloat(store.getAt(i).get('discount_rate'))/100 == "" ? 0 : parseFloat(store.getAt(i).get('discount_rate'))/100;
		discount_sums[discount_sums.length] = store.getAt(i).get('discount_amount') == "" ? 0 : store.getAt(i).get('discount_amount');
		discount_taxs[discount_taxs.length] = store.getAt(i).get('discount_tax') == "" ? 0 : store.getAt(i).get('discount_tax');		          		
	}           	
	//以上获取detail信息
    Share.AjaxRequest({
                url      : ctx + '/saveInvoice',
                params   : {
                	headId:head_id
                   ,num:num
                   ,date:date
                   ,type:type
                   ,mode:mode
                   ,custom:custom
                   ,customTax:custom_tax
                   ,address:address
                   ,account:account
                   ,custCode:custCode
                   ,remark:remark
                   ,seller:seller
                   ,sellerTax:seller_tax
                   ,sellerAddress:seller_address
                   ,sellerAccount:seller_account
                   ,field1:field1
                   ,field2:field2
                   ,field3:field3
                   ,field4:field4
                   ,field5:field5
                   ,field6:field6
                   ,field7:field7
                   ,field8:field8
                   ,field9:field9
                   ,field10:field10
                   ,field11:field11
                   ,field12:field12
                   ,field13:field13
                   ,field14:field14
                   ,field15:field15
                   ,field16:field16
                   ,field17:field17
                   ,field18:field18
                   ,field19:field19
                   ,field20:field20
                   ,payee:payee
                   ,checker:checker
                   ,drawer:drawer     
                   ,tAmount:TotalAmount
                   ,tTax:TotalTax
                   ,totalLine:totalLine
                   ,detail_ids:detail_ids
                   ,tax_items:tax_items                              
                   ,goods_names:goods_names
                   ,goods_codes:goods_codes
                   ,specs:specs
                   ,goods_units:goods_units
                   ,quantitys:quantitys
                   ,price_cures:price_cures
                   ,amount_curs:amount_curs
                   ,tax_rates:tax_rates
                   ,tax_amounts:tax_amounts
                   ,discount_rates:discount_rates
                   ,discount_sums:discount_sums
                   ,discount_taxs:discount_taxs
                   
               	   ,tp_mileages:new Array(store.getCount())
               	   ,goods_categorys:new Array(store.getCount())
               	   ,weights:new Array(store.getCount())
               	   ,weight_units:new Array(store.getCount())
               	   ,weight_discounts:new Array(store.getCount())
               	   ,weight_prices:new Array(store.getCount())
               	   ,volumes:new Array(store.getCount())
               	   ,volume_units:new Array(store.getCount())
               	   ,volume_prices:new Array(store.getCount())
               	   ,fee_amounts:new Array(store.getCount())
                   
                   ,count:count
                   ,itemIds : Ids.join(",")
                },
                callback : function(json) {
                    if (json.resultcode == '0') {
                    	Ext.Msg.alert(Share.T('提示'), Share.T('保存成功.'));
                     	cellEditing.cancelEdit();
                     	viewPanel();
                     	Ext.getCmp("invoice_count").setValue(json.count);
                     	Ext.getCmp('head_id').setValue(json.headId);
                     	var head_id = json.headId;
                     	if(head_id ==null||head_id==''){
                        	Ext.ComponentMgr.get("invoice_start").setValue(1);
                        }else{
                        	Ext.ComponentMgr.get("invoice_start").setValue(json.start);
                        }
                     	var store = Ext.getCmp('enter-invoice-grid').getStore();//获取grid信息
                        store.removeAll();
                        store.on('beforeload', function(store) {
                        	Ext.apply(store.proxy.extraParams , {document_no : num});
                        });
                        store.load();//grid信息更新
                        
                        /*更新history信息*/
	               		 var histStore = Ext.getCmp('deal-enter-history').getStore();
	               		 histStore.removeAll();
	               		 histStore.on('beforeload', function(store){
	               			 Ext.apply(store.proxy.extraParams , {document_no : json.docNo});
	               		 });
	               		 histStore.load();

                      Ext.getCmp('deal-enter-num').getStore().load();//基本信息-》票据号列表更新
                      
                      var searchgrid = Ext.getCmp('tab-deal-search');//若查询处理页面打开，刷新列表
  			          if (searchgrid != undefined) {
  			        	searchgrid.getStore().load();
  			        	searchgrid.getSelectionModel().deselectAll();
  			          }
                    } else if (json.resultcode == '1') {
                        Ext.Msg.alert(Share.T('提示'), Share.T('未登录'));
                    } else {
                        Ext.Msg.alert(Share.T('提示'), Share.T('保存失败.'));
                    }
                },
                falseFun : function(json) {// 失败后想做的个性化操作函数
                    Ext.Msg.alert(Share.T('提示'), Share.T('保存失败.'));
                }
            });
};

//页面相关部分的显示和隐藏
BTInv.panelHideShow = function(invConfig){
	/*1.销货单位框是否显示*/
    if(invConfig.SHOW_SELLER_INFO == 1){
		Ext.getCmp('deal-enter-seller').show();	 
		Ext.getCmp('seller_display').setValue(1);
	 }else{
		Ext.getCmp('deal-enter-seller').hide();
		Ext.getCmp('seller_display').setValue(0);
	 }
    /*2.进出口发票隐藏*/
    Ext.getCmp("deal-enter-export").hide();   
    /*3.运输发票框隐藏*/
    Ext.getCmp('deal-enter-trans').hide();
    /*4.明细grid特殊字段隐藏*/
	 Ext.getCmp('detail-tp-mileage').hide();
	 Ext.getCmp('detail-goods-category').hide();
	 Ext.getCmp('detail-weight').hide();
	 Ext.getCmp('detail-weight-unit').hide();
	 Ext.getCmp('detail-weight-discount').hide();
	 Ext.getCmp('detail-weight-price').hide();
	 Ext.getCmp('detail-volume').hide();
	 Ext.getCmp('detail-volume-unit').hide();
	 Ext.getCmp('detail-volume-price').hide();
	 Ext.getCmp('detail-fee-amount').hide();
};